import Vue from 'vue'
import Vuex from './vuex.js'

Vue.use(Vuex)

let store = new Vuex.Store({
  state: {
    queryStore: '',
    searchQueryType: 1,
    userId: 0,
    userName: '登录失败',
    collectionNum: 0,
    userLoginState: false,
    bookCollectState: 0,
    bookReportState: 0
  },
  getters: {
    getQueryStore(state) {
      return state.queryStore
    },
    getSearchQueryType(state) {
      return state.searchQueryType
    },
    getUserId(state){
      return state.userId
    },
    getUserName(state){
      return state.userName
    },
    getCollectionNum(state){
      return state.collectionNum
    },
    getUserLoginState(state){
      return state.userLoginState
    },
    getBookCollectState(state){
      return state.bookCollectState
    },
    getBookReportState(state){
      return state.bookReportState
    }
  },
  mutations: {
    refreshQueryStore(state, val) {
      state.queryStore = val
      state.searchQueryType = 1
    },
    refreshSearchQueryType(state, val) {
      state.queryStore = val
      state.searchQueryType = 2
    },
    refreshUserId(state,val){
      state.userId = val
    },
    refreshUserName(state,val){
      state.userName = val
    },
    refreshCollectionNum(state,val){
      state.collectionNum = val
    },
    setUserLoginState(state){
      if(state.userId !== 0){
        state.userLoginState = true
      } else{
        state.userLoginState = false
      }
    },
    refreshBookCollectState(state){
      if(state.bookCollectState == 0){
        state.bookCollectState = 1
      }else if(state.bookCollectState == 1){
        state.bookCollectState = 0
      }
    },
    initBookCollectState(state,val){
      state.bookCollectState = val
    },
    increCollectionNum(state){
      state.collectionNum = state.collectionNum + 1
    },
    decreCollectionNum(state){
      state.collectionNum = state.collectionNum - 1
    },
    refreshBookReportState(state){
      if(state.bookReportState == 0){
        state.bookReportState = 1
      }else if(state.bookReportState == 1){
        state.bookReportState = 0
      }
    },
    initBookReportState(state,val){
      state.bookReportState = val
    }
  },
  actions: {
    increase(context, price) {
      context.commit('increment', price)
    },
    setUserLoginStateFun(context){
      context.commit('setUserLoginState')
    }
  }
})

export default store;
